package com.lixh.virtual.ui.appList.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lixh.base.BaseFragment;
import com.lixh.utils.LoadingTip;
import com.lixh.view.LoadView;
import com.lixh.virtual.IExtra;
import com.lixh.virtual.R;
import com.lixh.virtual.app.os.bean.AppInfo;
import com.lixh.virtual.app.os.bean.AppInfoLite;
import com.lixh.virtual.ui.appList.adapter.AppListAdapter;
import com.lixh.virtual.ui.appList.presenter.AppListPresenter;
import com.lixh.virtual.utils.ItemOffsetDecoration;
import com.lixh.virtual.utils.VUiKit;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author lixh
 */
public class ListAppFragment extends BaseFragment<AppListPresenter> {


    @BindView(R.id.select_app_recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.select_app_install_btn)
    Button btInstance;
    AppListAdapter mAdapter;
    int type;

    public static ListAppFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(IExtra.KEY_APP_CLONE, type);
        ListAppFragment fragment = new ListAppFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initLoad(LoadView.Builder builder) {
        builder.hasToolbar = false;
        super.initLoad(builder);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_list_app;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        type = getArguments().getInt(IExtra.KEY_APP_CLONE, IExtra.APP_CLONE_INSTANCE);
        tip.setLoadingTip(LoadingTip.LoadStatus.LOADING);
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        mRecyclerView.addItemDecoration(new ItemOffsetDecoration(VUiKit.dpToPx(getContext(), 2)));
        mAdapter = new AppListAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mPresenter.getInstalledApps(type);
        btInstance.setOnClickListener(v -> {
            Integer[] selectedIndices = mAdapter.getSelectedIndices();
            ArrayList<AppInfoLite> dataList = new ArrayList<AppInfoLite>(selectedIndices.length);
            for (int index : selectedIndices) {
                AppInfo info = mAdapter.getItem(index);
                dataList.add(new AppInfoLite(info.packageName, info.path, info.fastOpen, info.isHook));
            }
            Intent data = new Intent();
            data.putParcelableArrayListExtra(IExtra.KEY_APP_INFO_LIST, dataList);
            getActivity().setResult(Activity.RESULT_OK, data);
            getActivity().finish();
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            int count = mAdapter.getSelectedCount();
            if (!mAdapter.isIndexSelected(position)) {
                if (count >= 9) {
                    Toast.makeText(getContext(), R.string.install_too_much_once_time, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            mAdapter.toggleSelected(position);
        });
    }

    public void setAppData(List<AppInfo> appInfos) {
        mAdapter.addData(appInfos);
        tip.setLoadingTip(LoadingTip.LoadStatus.FINISH);

    }
}
